//
//  ScrollViewController.swift
//  DemoUI
// scrollView 滚动视图
//  Created by 白瑾浩 on 2022/1/9.
//

import UIKit

class ScrollViewController: UIViewController, UIScrollViewDelegate {
    //var mMusicView:MusicView?
    var imgv1:UIImageView!
    var scrollView_img:UIScrollView!
    var itemHeight = 0.0
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = .white
        
        itemHeight = self.view.frame.size.height/4
        
//        mMusicView = MusicView()
//        mMusicView?.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height)
//        self.view.addSubview( mMusicView! )
//        mMusicView?.loadUI()

        let a:[UInt8] = [1,2,3,4,5]
        
        var d = NSMutableData()
        
        d.append( Data( a ) )
        
        let r:[UInt8] = []
        
        d.replaceBytes(in: NSRange(location: 0, length: 1), withBytes: r)
        
        print( d )
        
        let b = d.subdata(with: NSRange(location: 0, length: 3) )
        
        print( b )
        print( d )
        
        d.replaceBytes(in: NSRange(location: 0, length: 1), withBytes: nil ,length: 0)
        print( d )
        //基础使用
        
        //垂直滚动
        
        //初始化对象
        let scrollView = UIScrollView(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: itemHeight ) )

        //把对象插入到UIViewController中
        self.view.addSubview( scrollView )
        
        //初始化一个内容布局， 设置高为 屏幕高的2倍
        let subView1 = UIView(frame: self.view.frame )
        subView1.backgroundColor = UIColor.red
        subView1.frame.size.height = itemHeight*2
        
        //插入布局
        scrollView.addSubview( subView1 )
        
        //设置垂直滚动 宽度：屏幕宽度/高度：显示高度*2
        scrollView.contentSize = CGSize(width: self.view.frame.size.width, height:  itemHeight*2 )
        scrollView.alwaysBounceVertical = true //开启垂直方向的阻尼效果  所谓回弹效果
        scrollView.showsVerticalScrollIndicator = true //显示竖直方向滚动条
        //scrollView.delegate = self //添加代理  方便回调 进行位置计算等操作
        
        //横向滚动
        
        //初始化对象
        let scrollView_h = UIScrollView(frame: CGRect(x: 0, y: itemHeight, width: self.view.frame.size.width, height: itemHeight ) )

        //把对象插入到UIViewController中
        self.view.addSubview( scrollView_h )
        
        //初始化一个内容布局， 设置宽为 屏幕宽的2倍
        let subView_h = UIView(frame: self.view.frame )
        subView_h.backgroundColor = UIColor.blue
        subView_h.frame.size.height = itemHeight
        subView_h.frame.size.width = self.view.frame.size.width * 2
        
        //插入布局
        scrollView_h.addSubview( subView_h )
        
        //设置垂直滚动 宽度：屏幕宽度*2/高度：显示高度
        scrollView_h.contentSize = CGSize(width: CGFloat( self.view.frame.size.width*2 ), height:  itemHeight )
        scrollView_h.alwaysBounceHorizontal = true //开启水平方向的阻尼效果  所谓回弹效果
        scrollView_h.showsHorizontalScrollIndicator = true //显示水平方向滚动条
        //scrollView_h.delegate = self //添加代理  方便回调 进行位置计算等操作
        //开启自动分页效果,手指滑动将自动滑到下一页，方便做轮播方面的功能等
        scrollView_h.isPagingEnabled = true
        
        //显示图片
        //初始化对象
        scrollView_img = UIScrollView(frame: CGRect(x: 0, y: itemHeight*2, width: self.view.frame.size.width, height: itemHeight ) )

        //把对象插入到UIViewController中
        self.view.addSubview( scrollView_img )
        
        
        addImageView()
        

        
        scrollView_img.delegate = self //添加代理  方便回调 进行位置计算等操作
        scrollView_img.showsHorizontalScrollIndicator = false //隐藏水平方向滚动条
        scrollView_img.showsVerticalScrollIndicator = false //隐藏水平方向滚动条
        //当屏幕选装的时候自动调整布局
        //scrollView_img.autoresizingMask = UIView.AutoresizingMask.flexibleWidth | UIView.AutoresizingMask.flexibleHeight
        //防止点击顶部 滑到顶部的效果
        scrollView_img.scrollsToTop = false
        
        //注册捏合手势 ，以便操作图片双击变大  双击变小功能
        let doubleTap = UITapGestureRecognizer(target: self, action: #selector(handleDoubleTap))
        doubleTap.numberOfTapsRequired = 2
        scrollView_img.addGestureRecognizer( doubleTap )
        
        
//        //创建2个内容视图
//        let subView1 = UIView(frame: self.view.frame )
//        subView1.backgroundColor = UIColor.red
//        subView1.frame.size.height = self.view.frame.size.height*2
//
//
//        let subView2 = UIView(frame: CGRect(x: self.view.frame.size.width, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height ) )
//        subView2.backgroundColor = UIColor.blue
//
//        scrollView.addSubview( subView1 )
//        scrollView.addSubview( subView2 )
//        //设置横向滚动
//        scrollView.contentSize = CGSize(width: self.view.frame.size.width*2, height:  self.view.frame.size.height)
//        scrollView.contentSize = CGSize(width: self.view.frame.size.width*2, height:  self.view.frame.size.height*2 )
//        //阻尼效果
//        scrollView.bounces = true //关掉就没回弹的效果
//        scrollView.alwaysBounceVertical = false //开启垂直方向的阻尼效果
//        scrollView.alwaysBounceHorizontal = true //开启水平方向的阻尼效果
//
//        scrollView.showsVerticalScrollIndicator = true //显示竖直方向滚动条
//        scrollView.showsHorizontalScrollIndicator = true //显示水平方向滚动条
//
//        //开启自动分页效果
//        scrollView.isPagingEnabled = true
//
//        //代理协议 UIScrollViewDelegate  就是监听滑动式的相关数据
//        scrollView.delegate = self
    
        
    }
    func addImageView(){
        let img1 = UIImage(named: "2")  //在assets中的图片名称
        imgv1 = UIImageView(image: img1)
        scrollView_img.addSubview(imgv1)
        
        //设置
        scrollView_img.contentSize = imgv1.frame.size
        
        //在滚动视图中显示的图片的起始点是图片本身的（50，50）位置；根据图片高度和容器高度来计算显示图片中间部分
        scrollView_img.contentOffset = CGPoint(x:50, y:50) //向右偏移50，向下偏移50
        //表示最小缩放比例是原图的0.5倍，最大缩放比例是原图的2倍；
        scrollView_img.maximumZoomScale = 2.0
        
        //根据显示容器来计算缩放比例
        let ScaleHeight = itemHeight/imgv1.frame.size.height
        let ScaleWidth = self.view.frame.size.width/imgv1.frame.size.width
    
        
        scrollView_img.minimumZoomScale = min(ScaleWidth, ScaleHeight)
        
        scrollView_img.zoomScale = 1.0  //当前缩放比例为1
        
    }
    //双击图片变大变小
    @objc func handleDoubleTap(){
        if scrollView_img.zoomScale > self.scrollView_img.minimumZoomScale{
            scrollView_img.zoomScale = self.scrollView_img.minimumZoomScale
        }else{
            scrollView_img.zoomScale = self.scrollView_img.maximumZoomScale
        }
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //当滚动视图滚动时被系统自动调用
    }
//    每次缩放操作后，系统都会调用该方法用以告诉滚动视图zoomScale已改变。在上面的代码中，我们先计算出图片与屏幕间的填充。对于顶部和底部，先判断imageView的高是否小于scrollView的高，在imageView的高小于scrollView的高时，填充高度为两个视图高之差的二分之一，否则，填充为0；水平方向的填充与此类似。最后设定scrollView的contentInset属性，该属性用于指定内容视图与滚动视图边缘的距离，单位为point，默认值为UIEdgeInsetsZero。
    func scrollViewDidZoom(_ scrollView: UIScrollView) {
        //当滚动视图缩放时被系统自动调用
        // 计算imageView缩小到最小时 imageView与屏幕边缘距离
        let horizontalPadding = self.imgv1.frame.size.width < scrollView_img.frame.size.width ? (scrollView_img.frame.size.width - self.imgv1.frame.size.width) / 2 : 0
        
        let verticalPadding = self.imgv1.frame.size.height < scrollView_img.frame.size.height ? (scrollView_img.frame.size.height - self.imgv1.frame.size.height) / 2 : 0
    
        scrollView_img.contentInset = UIEdgeInsets(top: verticalPadding, left: horizontalPadding, bottom: verticalPadding, right: horizontalPadding)
    }
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        //当滚动视图将要开始拖动时被系统自动调用
    }
    //这个代理方法返回的View表示视图上发生缩放手势时，哪一个视图被缩放
    //在实现部分添加以下代理方法，指定可以缩放的视图为imageView。
    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return self.imgv1
    }
    //屏幕翻转调用重新写
    override func viewWillLayoutSubviews() {
       
        for view in scrollView_img.subviews{
            view.removeFromSuperview()
        }
        addImageView()
        
    }
    
}

